package com.rzgeo.workmap;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.Menu;

import com.esri.arcgisruntime.mapping.view.SceneView;
import com.rzgeo.rzmaplib.comp.RmlLayerList;
import com.rzgeo.rzmaplib.comp.RmlPanel;
import com.rzgeo.rzmaplib.comp.RmlSceneToolKit;
import com.rzgeo.rzmaplib.comp.geoview.RmlSceneViewHolder;
import com.rzgeo.rzmaplib.model.RmlEnv;
import com.rzgeo.rzmaplib.model.RmlPrj;
import com.rzgeo.rzmaplib.model.RmlPrjMapLoader;
import com.rzgeo.rzmaplib.util.IRmlLoadListener;
import com.rzgeo.rzmaplib.util.IRmlSerializable;

public class SceneActv extends AppCompatActivity {

    private RmlSceneViewHolder _geoViewHolder;
    private RmlPrjMapLoader _mapLoader;
    private RmlSceneToolKit _sceneTool;
    private RmlLayerList _layerList;
    private RmlPanel _leftPanel;
    private RmlPanel _rightPanel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        StartInfo info=
                (StartInfo) getIntent().getSerializableExtra(StartInfo.Key);
        RmlPrj prj= RmlEnv.getInstance().getAppDir().prjsSet.getPrj(info.prjName);

        setContentView(R.layout.actv_scene);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        _geoViewHolder = new RmlSceneViewHolder((SceneView) findViewById(R.id.sceneview));

        _sceneTool=findViewById(R.id.scenetool);
        _sceneTool.setup((SceneView)_geoViewHolder.getGeoView());

        _leftPanel=findViewById(R.id.panel_left);
        _leftPanel.setup(findViewById(R.id.btn_closeleft));
        _rightPanel=findViewById(R.id.panel_right);
        _rightPanel.setup(findViewById(R.id.btn_closeright));

        _mapLoader =new RmlPrjMapLoader(this, prj,_geoViewHolder, new IRmlLoadListener() {
            @Override
            public void Loaded(boolean success, String msg) {
                _mapLoader.getFullExtent();
            }
        });

        _layerList=findViewById(R.id.layerlist);
        _layerList.setMapModel(this,_geoViewHolder,null,null);

//        FloatingActionButton fab = findViewById(R.id.fab);
//        fab.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
//                        .setAction("Action", null).show();
//            }
//        });
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {

            new AlertDialog.Builder(this)
                    .setTitle("是否离开项目")
                    .setPositiveButton("离开", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            finish();
                        }
                    })
                    .setNegativeButton("留下", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    }).show();
            return true;//不执行父类点击事件
        }
        return super.onKeyDown(keyCode, event);//继续执行父类其他点击事件
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        _mapLoader.release();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.actv_scene, menu);

        return super.onCreateOptionsMenu(menu);
    }

    public static class StartInfo implements IRmlSerializable {
        public String prjName;

        public StartInfo(String name) {
            prjName=name;
        }
    }
}
